﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;
using KPIS.GERP.GAMS.DAL;
using KPIS.GERP.GAMS.MODEL;
using System.Collections.Generic;

namespace KPIS.GERP.GAMS.APP.App_UI.Forms
{
    public partial class ACM_FINANCE_RECEIVE : System.Web.UI.Page
    {
        Commons.Main main = new Commons.Main();
        M_ACM_FIN_MAS_RECEIVE_Type m_acm_fin_mas_receive = new M_ACM_FIN_MAS_RECEIVE_Type();
        D_ACM_Finance_Receive d_acm_fin_receive = new D_ACM_Finance_Receive();
        D_ACM_MAS_Account d_acm_mas_account = new D_ACM_MAS_Account();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UpdateUI1("Load");
               // Session["UserID"] = "555";
               // LoadFilterToAccountCode();
                //cmbAccountCode.Items.Clear();
                //cmbAccountCode.Text = "";
            }
        }
        //protected void cmbAccountCode_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        //{
        //    txtAccountCode.Text = cmbAccountCode.SelectedValue;
        //}
        //protected void LoadFilterToAccountCode()
        //{
        //    cmbAccountCode.DataSource = d_acm_mas_account.GetListAccount();
        //    cmbAccountCode.DataBind();
        //    main.InsertEmptyDDLSelect(cmbAccountCode);
        //}
        //protected void LoadFilterToAccountCode()
        //{
        //    cmbAccountCode.DataSource = d_acm_mas_account.GetListAccount();
        //    cmbAccountCode.DataBind();
        //    main.InsertEmptyDDLSelect(cmbAccountCode);
        //}
        protected void cmbAccountCode_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            IList<M_ACM_MAS_Account> infoList = d_acm_mas_account.GetListDataAccountFilter(strFilter);
            int itemOffset = e.NumberOfItems;
            int endOffset = Math.Min(itemOffset + 10, infoList.Count);
            e.EndOfItems = endOffset == infoList.Count;

            for (int i = itemOffset; i < endOffset; i++)
            {
                cmbAccountCode.Items.Add(new RadComboBoxItem(infoList[i].account_code_name, infoList[i].account_seq.ToString()));
            }

            e.Message = main.GetStatusMessage(endOffset, infoList.Count);
        }
        protected void grdACMReceiveFinance_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
        {
            if (!e.IsFromDetailTable)
            {
                RadGridFinance.DataSource = d_acm_fin_receive.GetList();
            }
        }
        protected void grdACMReceiveFinance_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            
            if (e.CommandName == "DataEdit")
            {
                cmbAccountCode.Focus();
                UpdateUI1("Update");
                M_ACM_FIN_MAS_RECEIVE_Type info = d_acm_fin_receive.GetListBySeq(main.ConvertNullInt(RadGridFinance.Items[e.Item.ItemIndex]["receive_type_seq"].Text));
                txtBgmSourceName.Text = info.bgm_source_name;
                txtReceiveTypeName.Text = info.receive_type_name;
                txtAccountCode.Text = info.account_code;
                hReceiveTypeSeq.Text = info.receive_type_seq.ToString();
                hAccountSeq.Text = info.account_seq.ToString();
                M_ACM_MAS_Account infoacc = d_acm_mas_account.GetListFillByAccountSeq(info.account_seq);
                cmbAccountCode.Text = infoacc.account_code_name;
            }
        }

        protected void RadAjaxManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            string strArg;
            strArg = e.Argument;
            cmbAccountCode.SelectedValue = strArg;
            M_ACM_MAS_Account infoacc = d_acm_mas_account.GetListFillByAccountSeq(Convert.ToInt32(strArg));
            cmbAccountCode.Text = infoacc.account_code_name;
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (checkValidate())
            {
                UpdateUI1("Save");
                m_acm_fin_mas_receive.receive_type_seq = Convert.ToInt32(hReceiveTypeSeq.Text) ;
                m_acm_fin_mas_receive.account_seq = main.ConvertNullInt(cmbAccountCode.SelectedValue);//Convert.ToInt32(txtAccountCode.Text);
                m_acm_fin_mas_receive.updated_by = main.ConvertNullInt(main.strEmpID); ;
                m_acm_fin_mas_receive.updated_when = DateTime.Now;
                d_acm_fin_receive.Update(m_acm_fin_mas_receive);
                RadGridFinance.DataSource = d_acm_fin_receive.GetList();
                RadGridFinance.DataBind();
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            UpdateUI1("Cancel");
        }
       
        //============================ control events ==============================
        protected void UpdateUI1(string strCommand)
        {
            switch (strCommand)
            {
                case "Load":
                    lblStatus.Text = "แสดงสถานะการทำงาน";
                    main.StrSelectWord = "< เลือกรายการรหัสบัญชี >";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    btnAccountCode.Enabled = false;
                    txtAccountCode.Enabled = false;
                    cmbAccountCode.Enabled = false;
                    ClearObj();
                    break;
                case "Update":
                    lblStatus.Text = "แก้ไขข้อมูลจากที่เลือก";
                    btnSave.Enabled = true;
                    btnCancel.Enabled = true;
                    btnAccountCode.Enabled = true;
                    cmbAccountCode.Enabled = true;
                    txtAccountCode.Enabled = true;
                    break;
                case "Save":
                    lblStatus.Text = "บันทึกข้อมูลเรียบร้อยแล้ว";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    btnAccountCode.Enabled = false;
                    cmbAccountCode.Enabled = false;
                    txtAccountCode.Enabled = false;
                    break;
                case "Cancel":
                    lblStatus.Text = "ยกเลิกข้อมูล";
                    btnSave.Enabled = false;
                    btnCancel.Enabled = false;
                    btnAccountCode.Enabled = false;
                    cmbAccountCode.Enabled = false;
                    txtAccountCode.Enabled = false;
                    ClearObj();
                    break;
            }
        }
        //============================ Validate ==============================
        private Boolean checkValidate()
        {
            Boolean bFlag = true;
            string strAccountSeq = cmbAccountCode.SelectedValue;

            if (strAccountSeq.Length == 0)
            {
                lblStatus.Text = "กรุณาเลือกรหัสบัญชีด้วย";
                cmbAccountCode.Focus();
                bFlag = false;
            }
            
            return bFlag;
        }
        private void ClearObj()
        {
            txtAccountCode.Text = "";
            txtBgmSourceName.Text = "";
            txtReceiveTypeName.Text = "";
            cmbAccountCode.SelectedValue = "";
        }
     
    }
}
